import domtoimage from 'dom-to-image'

function toImage (id, isType, quality) {
  const node = document.getElementById(id)
  const qualityIs = quality || 1
  const type = isType || 'png'

  if (type === 'png' || type === 'PNG' || type === 'Png') {
    return new Promise((resolve, reject) => {
      domtoimage.toPng(node)
        .then(url => {
          resolve(url)
        })
        .catch(err => {
          reject(err)
        })
    })
  } else if (type === 'jpeg' || type === 'jpg' || type === 'JPG' || type === 'JPEG' || type === 'Jpeg' || type === 'Jpg') {
    return new Promise((resolve, reject) => {
      domtoimage.toJpeg(node, { quality: qualityIs })
        .then(url => {
          resolve(url)
        })
        .catch(err => {
          reject(err)
        })
    })
  }
}

export default toImage
